package com.kitty.game.rank.service.handler;

import com.kitty.game.role.model.Role;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.user.RoleTowerData;
import com.kitty.game.rank.model.RankType;
import com.kitty.game.rank.model.pojo.DataUnit;
import com.kitty.game.rank.model.pojo.RankGroupListData;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.model.record.TowerMirrorRankRecord;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**通天塔镜像排行榜处理类*/
@Component
@Slf4j
public class TowerMirrorRankHandler extends RangeRankHandler {
    private final int minLevel = 50;
    private final int maxLevel = 139;

    @PostConstruct
    public void initSize() {
        setRankSize(7);
        setCacheSize(7);
    }

    /**从数据库加载排行榜数据*/
    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        RankGroupListData<TowerMirrorRankRecord> rankGroupListData = dao.fetch(RankGroupListData.class, RankType.TOWER_MIRROR.name());
        if (rankGroupListData != null) {
            rankGroupListData.doAfterInit(TowerMirrorRankRecord.class);

            List<DataUnit> rankData = rankGroupListData.getRankData();
            for (DataUnit dataUnit : rankData) {
                String levelRange = dataUnit.getKey();
                for (RankRecord record : dataUnit.getRecords()) {
                    TowerMirrorRankRecord towerRecord = (TowerMirrorRankRecord) record;
                    update(levelRange, towerRecord.getGid(), record);
                }
            }
        }
    }

    public void save() {
        List<DataUnit> rankData = new ArrayList<>();
        for (int level=minLevel; level<=maxLevel; level++) {
            String levelRange = String.valueOf(level);
            List<RankRecord> records = getRecordsFrom(levelRange, 0, getRankSize());
            if (records == null || records.size() == 0) {continue ;}

            DataUnit dataUnit = new DataUnit(levelRange, records);
            rankData.add(dataUnit);
        }

        if (rankData.size() <= 0) {return ;}

        RankGroupListData<TowerMirrorRankRecord> rankGroupListData = new RankGroupListData<>(RankType.TOWER_MIRROR, rankData);
        rankGroupListData.doBeforeSave();
        Dao dao = SpringUtils.getBean(Dao.class);
        dao.insertOrUpdate(rankGroupListData);
    }

    @PreDestroy
    private void shutDown() {
        save();
        log.error("[{}]保存数据后关闭", this.getClass().getSimpleName());
    }


    public TowerMirrorRankRecord getRecord(int layer, int npcId) {
        String levelRange = String.valueOf(layer);
        List<RankRecord> records = getRecordsFrom(levelRange, 0, getRankSize());
        for (RankRecord rankRecord : records) {
            if (((TowerMirrorRankRecord)rankRecord).getNpcId() == npcId) {
                return (TowerMirrorRankRecord)rankRecord;
            }
        }

        return null;
    }

    public void update(Role role, List<Integer> npcIds) {
        RoleTowerData roleTowerData = role.getActivity().getRoleTowerData();
        int npcId = getNpcId(role, npcIds);
        int layer = roleTowerData.getCurrentLayer();
        update(role, npcId, layer);
    }

    private int getNpcId(Role role, List<Integer> npcIds) {
        String levelRange = String.valueOf(role.getLevel());
        List<RankRecord> records = getRecordsFrom(levelRange, 0, getRankSize());
        TowerMirrorRankRecord record = getContainRecord(role, records);
        if (record != null) {
            return record.getNpcId();
        }
        if (records.size() < npcIds.size()) {
            return npcIds.get(records.size());
        } else {
            TowerMirrorRankRecord rankRecord = (TowerMirrorRankRecord)records.get(records.size() - 1);
            return rankRecord.getNpcId();
        }
    }

    private TowerMirrorRankRecord getContainRecord(Role role, List<RankRecord> records) {
        for (RankRecord rankRecord : records) {
            if (((TowerMirrorRankRecord)rankRecord).getGid().equals(role.getGid())) {
                return (TowerMirrorRankRecord)rankRecord;
            }
        }

        return null;
    }

    private void update(Role role, int npcId, int layer) {
        String levelRange = String.valueOf(role.getLevel());
        this.update(levelRange, role.getGid(), new TowerMirrorRankRecord(npcId, role.getGid(), role.getName(), role.getRoleIcon(), layer, (short) role.getWeaponIcon(), role.getSuitId(), role.getSuitLightId()));
    }
}
